﻿using HalconDotNet;
using Halcon二次开发.Model.储放所有单例.储存每个单例界面;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Halcon二次开发.View.图片显示弹窗
{
    /// <summary>
    /// 单例模式
    /// </summary>
    public partial class ImageToolForm : Form
    {
        static ImageToolForm instance;  
        /// <summary>
        /// 单例模式
        /// </summary>
        /// <param name="obj">路径</param>
        /// <param name="okng">OK还是NG图片</param>
        /// <returns></returns>
        public static ImageToolForm GetSingle(object obj,object okng) {
            if (instance == null)
            {
                instance = new ImageToolForm();
            }
            //窗体自动获得焦点
            instance.BringToFront();        
            instance.Focus(); 
            
            instance.SetImage(obj as Image,okng);
            return instance;
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="obj">路径</param>
        /// <param name="okng">OK还是NG图片</param>
        private ImageToolForm()
        {
            InitializeComponent();
            hSmartWindowControl1.MouseWheel += HSmartWindowControl1_MouseWheel;
        }
        /// <summary>
        /// 单例模式切换图片
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="okng"></param>
        public void SetImage(Image obj, object okng) {
            try
            {
                HOperatorSet.ClearWindow(hSmartWindowControl1.HalconWindow);
                //  HOperatorSet.ReadImage(out HObject image, obj.ToString());
                // hSmartWindowControl1.HalconWindow.DispObj(image);
              
                hSmartWindowControl1.HalconWindow.DispObj(ConvertToHImage(obj));
                if (okng.ToString() == "OK")
                {
                    HOperatorSet.DispText(hSmartWindowControl1.HalconWindow, okng.ToString(), "window", "top", "center", "green", null, null);
                }
                else HOperatorSet.DispText(hSmartWindowControl1.HalconWindow, okng.ToString(), "window", "top", "center", "red", null, null);
                hSmartWindowControl1.SetFullImagePart();
            }
            catch
            {
                ((Form1)FormDb.Db["Form1"]).textBox1.AppendText("图片获取失败,可能路径错误line-ImageForm.cs-32!\r\n");
            }
        }
        public  HObject ConvertToHImage(Image netImage)
        {
            // 保存.NET Image为临时文件  
            string tempFilePath = System.IO.Path.GetTempFileName() + ".bmp";
            netImage.Save(tempFilePath, ImageFormat.Bmp);

            // 从临时文件读取图像到Halcon HImage对象  
            HOperatorSet.ReadImage(out HObject halconImage, tempFilePath);

            //可选：删除临时文件
             System.IO.File.Delete(tempFilePath);

            return halconImage;
        }

        private void HSmartWindowControl1_MouseWheel(object sender, MouseEventArgs e)
        {
            hSmartWindowControl1.HSmartWindowControl_MouseWheel(sender, e);
        }

        private void ImageToolForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Hide();
            e.Cancel = true;
        }
    }
}
